package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysGoods;
import com.ruoyi.system.domain.SysOrder;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysGoodsService;
import com.ruoyi.system.service.ISysOrderService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2021-05-21
 */
@Controller
@RequestMapping("/system/order")
public class SysOrderController extends BaseController
{
    private String prefix = "system/order";

    @Autowired
    private ISysOrderService sysOrderService;

    @Autowired
    private ISysGoodsService sysGoodsService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysDeptService sysDeptService;

    @RequiresPermissions("system:order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/order";
    }

    /**
     * 查询订单列表
     */
    @RequiresPermissions("system:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysOrder sysOrder)
    {

        List<SysOrder> list = null;
        Long userId = ShiroUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (userId != 1l) {
            sysOrder.setSysDept1(sysUser.getDeptId());
            startPage();
            List<SysOrder> list1 = sysOrderService.selectSysOrderList(sysOrder);
            sysOrder.setSysDept1(null);
            sysOrder.setSysDept2(sysUser.getDeptId());
            startPage();
            List<SysOrder> list2 = sysOrderService.selectSysOrderList(sysOrder);
            list1.addAll(list2);
            list = list1;
            return getDataTable(list);
        }
        startPage();
        list = sysOrderService.selectSysOrderList(sysOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("system:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysOrder sysOrder)
    {
        List<SysOrder> list = sysOrderService.selectSysOrderList(sysOrder);
        ExcelUtil<SysOrder> util = new ExcelUtil<SysOrder>(SysOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 订单结束后设置状态为1
     * @param id
     * @return
     */
    @PostMapping("/orderEnd")
    @ResponseBody
    public AjaxResult orderEnd(@RequestParam("id") String id) {

        Long oid = Long.valueOf(id);
        SysOrder sysOrder = sysOrderService.selectSysOrderById(oid);
        sysOrder.setMyStatus(1);

        return toAjax(sysOrderService.updateSysOrder(sysOrder));
    }

    /**
     * 去查看详情页
     * @return 数据
     */
    @GetMapping("/toDetail/{id}")
    public String toDetails(@PathVariable("id") Long id, ModelMap mmap, Model model) {
        SysOrder sysOrder = sysOrderService.selectSysOrderById(id);
        Long goodsId = sysOrder.getGoodsId();
        SysGoods sysGoods = sysGoodsService.selectSysGoodsById(goodsId);
        Long sysDept3 = sysOrder.getSysDept1();
        Long sysDept4 = sysOrder.getSysDept2();
        SysDept sysDept1 = sysDeptService.selectDeptById(sysDept3);
        SysDept sysDept2 = sysDeptService.selectDeptById(sysDept4);

        model.addAttribute("sysOrder",sysOrder);
        mmap.put("sysDept1",sysDept1);
        mmap.put("sysDept2",sysDept2);
        mmap.put("sysOrder",sysOrder);
        mmap.put("sysGoods",sysGoods);
        return prefix + "/detail";
    }



    /**
     * 新增订单
     */
    @GetMapping("/add/{id}")
    public String add(ModelMap mmap, @PathVariable("id") Long id)
    {
        SysGoods sysGoods = sysGoodsService.selectSysGoodsById(id);
        Long userId = ShiroUtils.getUserId();

        SysUser sysUser = sysUserService.selectUserById(userId);

        mmap.put("user",sysUser);
        mmap.put("good",sysGoods);
        return prefix + "/add";
    }

    /**
     * 新增保存订单
     */
    @RequiresPermissions("system:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysOrder sysOrder)
    {
        sysOrder.setMyStatus(2);
        Long goodsId = sysOrder.getGoodsId();
        SysGoods sysGoods = sysGoodsService.selectSysGoodsById(goodsId);
        sysGoods.setStock(sysGoods.getStock() - sysOrder.getGoodsNum());
        sysGoodsService.updateSysGoods(sysGoods);
        new Thread(()->{
            try {
                Thread.sleep(22000);
                orderEnd(sysOrder.getId().toString());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        return toAjax(sysOrderService.insertSysOrder(sysOrder));
    }

    /**
     * 修改订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysOrder sysOrder = sysOrderService.selectSysOrderById(id);
        mmap.put("sysOrder", sysOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单
     */
    @RequiresPermissions("system:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysOrder sysOrder)
    {
        return toAjax(sysOrderService.updateSysOrder(sysOrder));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("system:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysOrderService.deleteSysOrderByIds(ids));
    }
}
